<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>后台模板SpringbootDemo版本</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="/static/css/font.css">
        <link rel="stylesheet" href="/static/css/xadmin.css">
        <link rel="stylesheet" href="/static/ztree/css/metroStyle/metroStyle.css">
        <script src="/static/js/jquery.min.js" charset="utf-8"></script>
        <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
        <script src="/static/js/ztree.js" charset="utf-8"></script>
        <script src="/static/js/excheck.js" charset="utf-8"></script>
        <script type="text/javascript" src="/static/js/xadmin.js"></script>
    </head>
    <body>


        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label for="roleName" class="layui-form-label">
                            角色名称
                        </label>
                        <div class="layui-input-inline" >
                          <input type="text" id="roleName" name="roleName" required=""
                                   autocomplete="off" class="layui-input">
                        </div>

                    </div>
                    <div class="layui-form-item">
                        <label for="remark" class="layui-form-label">
                            <span class="x-red">*</span>备注
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="remark" name="remark" required="" lay-verify="required"
                                   autocomplete="off" class="layui-input">
                        </div>

                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            菜单权限
                        </label>
                        <div class="layui-input-inline">
                            <ul id="menuTree" class="ztree"></ul>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button  class="layui-btn" lay-filter="add" lay-submit="" style="margin-left: 111px;">
                            提交
                        </button>
                    </div>

                </form>
            </div>
        </div>
        <script>
            var setting = {
                check: {
                    enable: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                async: {
                    enable: true,
                    url: "/role/getRoleMenu/-1"
                },
                data:{
                    key:{
                        name:'menuName',
                        children:'treeBeanList'
                    }
                }

            };

            var zNodes = [];
            $(document).ready(function(){
                $.fn.zTree.init($("#menuTree"), setting, zNodes);
            });
            layui.use(['form', 'layer'],
               function() {
                    $ = layui.jquery;
                    var form = layui.form,
                    layer = layui.layer;
                    //监听提交
                    form.on('submit(add)',function(data){
                        //获取所有的选中菜单的id
                        var checkedNode = "";
                        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
                        var nodes = treeObj.getCheckedNodes(true);
                        for(var i = 0 ; i < nodes.length; i ++){

                            if(checkedNode == ""){
                                checkedNode = nodes[i].id;
                            }else{
                                checkedNode += ","+nodes[i].id;
                            }
                        }

                        $.ajax({
                            url:"/role/add",
                            type:"post",
                            dateType:'json',
                            data:{
                                "menuIds":checkedNode,
                                "roleName" : data.field.roleName,
                                "remark" : data.field.remark
                            },
                            success:function(result){
                                if(result.code == 0){
                                    layer.alert("修改",{icon:6},function(){
                                        //关闭当前frame
                                        xadmin.close();
                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });

                                }

                            }
                        });
                        return false;
                    });




            });
        </script>

    </body>

</html>
